let debug = 0;

function pushStackValue(stack, dp, value) {
    if (dp > 0) {
        pushStackValue(stack[stack.length - 1].value, dp - 1, value);
    } else {
        stack.push(value);
    }
}

//将语法树铺平解析成字符串
let spread = (s, p = "") => {
    return (
        s
            .map((o) => {
                if (typeof o == "object") {
                    if (debug) console.log("###", o); //debug

                    return spread(o.value, p ? `${p}.${o.name}` : o.name);
                } else {
                    return o ? (p ? `${p}.${o}` : o) : null;
                }
            })
            //去掉空的和.结尾的
            .filter((s) => !!s && !/\.$/.test(s))
            .join(",")
    );
};

function spreadPaths(p) {
    let m;
    let stack = [];
    let deep = 0;

    if ({}.toString.call(p) !== "[object String]") {
        throw new Error("需要传入参数类型为字符串");
    }

    function match(re) {
        var m = re.exec(p);
        if (!m) return;
        p = p.slice(m[0].length);
        return m;
    }

    while (p) {
        //不含,的字符
        if ((m = match(/^[^,]*/))) {
            if (debug) console.log("@:" + m[0]); //debug
            //包含[ 或 ]
            if (/\[|\]/g.test(m[0])) {
                if (/\](?!\s*(\,|\]|$))/.test(m[0].trim())) {
                    throw new Error(`语法有误，不支持聚合操作:${m[0]}`);
                }
                m[0]
                    .replace(/([^\[\]]*)\[/g, (a, b, c) => {
                        if (debug)
                            console.log(`==>>'${a}', b:'${b}', c:'${c}'`); //debug

                        //匹配 test[
                        pushStackValue(stack, deep, {
                            name: b.trim(),
                            value: [],
                        });
                        deep++;
                        return "";
                    })

                    .replace(/([^\[\]]*)\]/g, (a, b, c) => {
                        if (debug)
                            console.log(`<<=='${a}', b:'${b}', c:'${c}'`); //debug
                        //匹配 test]
                        if (b.trim()) {
                            pushStackValue(stack, deep, b.trim());
                        }
                        deep--;
                        return "";
                    })
                    //匹配 test
                    .replace(/[^\[\]]*/g, (a) => {
                        if (a.trim()) {
                            pushStackValue(stack, deep, a.trim());
                        }
                        return "";
                    });
            } else {
                //不包含括号直接加入
                pushStackValue(stack, deep, m[0].trim());
            }
        }
        //去掉逗号，进行下次元素匹配
        match(/^,\s*/);
    }
    if (debug) console.log(deep, JSON.stringify(stack)); //debug

    //遇到[深度+1，遇到]深度-1，因此，深度不为0 表示括号未闭合
    if (deep !== 0) {
        throw new Error("语法有误，括号未闭合");
    }

    // console.log(JSON.stringify(stack));

    return spread(stack); //.split(",");
}

module.exports = spreadPaths;
